//
//  MeViewController.m
//  SmartCup
//
//  Created by Kendama on 2017/5/17.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "MeViewController.h"

@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UIImageView *topView;
@property (strong, nonatomic) UIImageView *avatarView;
/** 编辑按钮 **/
@property(nonatomic,strong) UIButton *editButton;
@property(nonatomic,strong) UIButton *settingButton;

@property (strong, nonatomic) UILabel *nicknameLabel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *listArray;
@property (nonatomic, strong) NSArray *imageListArray;


@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)refreshBaseControlValue {
    [self setNavigationBarTitle:@"我的"];
    [self hideNavigationBottomLine];
    [self masLayoutSubviews];
    
}


#pragma -- 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return px1920Height(168);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return px1920Height(40);
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * identifier = @"meCell";
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    //右侧小箭头
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    cell.textLabel.text = self.listArray[indexPath.row];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"#1E1D1C"];
    cell.textLabel.font = [UIFont systemFontOfSize:px1080Width(42)];
    
    cell.imageView.image = [UIImage imageNamed:self.imageListArray[indexPath.row]];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.hidesBottomBarWhenPushed = YES;
    if (indexPath.row == 0) {
        WebViewController *vc = [[WebViewController alloc] init];
        vc.title = @"常见问题";
        vc.type = 1;
        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.row == 1) {
        FeedbackViewController *vc = [[FeedbackViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.row == 2) {
        AboutWeViewController *vc = [[AboutWeViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    self.hidesBottomBarWhenPushed = NO;

}

#pragma mark - 点击事件
- (void)didClickSettingButton
{
    self.hidesBottomBarWhenPushed = YES;
    SettingViewController *vc = [[SettingViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

- (void) didClickEditButton {
    self.hidesBottomBarWhenPushed = YES;
    UserViewController *vc = [[UserViewController alloc] init];
    [vc setUpdateBlock:^{
        [self.topView sd_setImageWithURL:[NSURL URLWithString:DTCEXRACT_OBJECT(IMAGE)]];
        [self.avatarView sd_setImageWithURL:[NSURL URLWithString:DTCEXRACT_OBJECT(IMAGE)]];

        _nicknameLabel.text = DTCEXRACT_OBJECT(NICKNAME);

    }];
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;

}


#pragma mark -- 懒加载
- (UIImageView *)topView
{
    if(!_topView)
    {
        UIView *superView = self.view;
        _topView = [[UIImageView alloc]init];
        [superView addSubview:_topView];
        [_topView sd_setImageWithURL:[NSURL URLWithString:DTCEXRACT_OBJECT(IMAGE)]];
        UIToolbar *toolbar = [[UIToolbar alloc] init]; // 添加毛玻璃效果
        toolbar.barStyle = UIBarStyleBlack;
        [_topView addSubview:toolbar];
        [toolbar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(_topView.center);
            make.width.height.mas_equalTo(_topView);
        }];
        _topView.userInteractionEnabled = YES;
    }
    return _topView;
}


- (UIImageView *)avatarView
{
    if(!_avatarView)
    {
        UIView *superView = self.view;
        _avatarView = [[UIImageView alloc]init];
        _avatarView.layer.masksToBounds = YES;
        _avatarView.layer.cornerRadius = px1080Width(406) / 2 ;
        [superView addSubview:_avatarView];
        [_avatarView sd_setImageWithURL:[NSURL URLWithString:DTCEXRACT_OBJECT(IMAGE)] placeholderImage:[UIImage imageNamed:@"icon_user"]];
        [self.topView addSubview:_avatarView];
    }
    return _avatarView;
}


- (UILabel *)nicknameLabel
{
    if(!_nicknameLabel)
    {
        UIView *superView = self.topView;
        _nicknameLabel = [[UILabel alloc]init];
        [superView addSubview:_nicknameLabel];
        _nicknameLabel.textAlignment = NSTextAlignmentCenter;
        [_nicknameLabel setFont:[UIFont systemFontOfSize:px1080Width(64)]];
        [_nicknameLabel setTextColor:[UIColor whiteColor]];
        _nicknameLabel.text = @"我是昵称";
        _nicknameLabel.text = DTCEXRACT_OBJECT(NICKNAME);
    }
    return _nicknameLabel;
}

- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        [superView addSubview:_tableView];
    }
    return _tableView;
}


- (NSArray *)listArray
{
    if(!_listArray)
    {
        _listArray = @[@"常见问题",@"意见反馈",@"关于我们"];

    }
    return _listArray;
}


- (NSArray *)imageListArray
{
    if(!_imageListArray)
    {
        _imageListArray = @[@"icon_preplem",@"icon_opinion",@"icon_we"];
    }
    return _imageListArray;
}


- (UIButton *)editButton
{
    if(!_editButton)
    {
        UIView *superView = self.view;
        _editButton = [[UIButton alloc]init];
        [superView addSubview:_editButton];
        [_editButton setImage:[UIImage imageNamed:@"icon_edit1"] forState:UIControlStateNormal];
        [_editButton setImage:[UIImage imageNamed:@"icon_edit1_pre"] forState:UIControlStateHighlighted];
        [_editButton addTarget:self action:@selector(didClickEditButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editButton;
}
- (UIButton *)settingButton
{
    if(!_settingButton)
    {
        UIView *superView = self.view;
        _settingButton = [[UIButton alloc]init];
        [superView addSubview:_settingButton];
        [_settingButton setImage:[UIImage imageNamed:@"icon_set up"] forState:UIControlStateNormal];
        [_settingButton setImage:[UIImage imageNamed:@"icon_set up_pre"] forState:UIControlStateHighlighted];
        [_settingButton addTarget:self action:@selector(didClickSettingButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _settingButton;
}

#pragma mark -- 初始化
- (void)masLayoutSubviews{
    __weak UIView *superView = self.view;
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superView);
        make.width.equalTo(superView);
        make.height.mas_equalTo(px1920Height(989));
    }];
    
    [self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.topView);
        make.height.width.mas_equalTo(px1080Width(406));
    }];
    
    [self.nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarView.mas_bottom).with.offset(px1920Height(48));
        make.width.equalTo(self.topView);
        make.height.mas_equalTo(px1920Height(50));
    }];
    
    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.avatarView.mas_centerY);
        make.right.mas_equalTo(self.avatarView.mas_left).with.offset(-px1080Width(114));
        make.height.mas_equalTo(px1920Height(108));
        make.width.mas_equalTo(px1080Width(108));
    }];
    
    [self.settingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.avatarView.mas_centerY);
        make.left.mas_equalTo(self.avatarView.mas_right).with.offset(px1080Width(114));
        make.height.mas_equalTo(px1920Height(108));
        make.width.mas_equalTo(px1080Width(108));
    }];
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom);
        make.width.equalTo(self.topView);
        make.bottom.equalTo(self.view);
    }];
}


@end
